package com.dxq.test.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.dxq.test.common.AppManager;
import com.dxq.test.R;
import com.dxq.test.db.TablesDao;
import com.dxq.test.fragment.Fragment1_Pasture;
import com.dxq.test.fragment.Fragment2_Inventory;
import com.dxq.test.fragment.Fragment3_Event;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private FragmentManager mFm; //fragment manager
    private FragmentTransaction mFt;//fragment transaction
    private Fragment1_Pasture fragment1;
    private Fragment2_Inventory fragment2;//
    private Fragment3_Event fragment3;//
    private Fragment mFragmentCurrent;

    private ImageView ivBack;
    private TextView textView_title;
    private TextView textView_f1;
    private TextView textView_f2;
    private TextView textView_f3;

    public TablesDao tablesDao;

    private long exitTime = 0;//记录时间

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //添加Activity到堆栈
        AppManager.getAppManager().addActivity(this);
        setContentView(R.layout.activity_main);
        initView();
        tablesDao = new TablesDao(this);
        //初始化牧场
//        Pasture p = new Pasture();
//        p.setName("默认牧场");
//        p.setLabels(null);
//        tablesDao.insertPasture(p);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在appManager结束当前activity
        AppManager.getAppManager().finishActivity(this);
    }


    private void initView() {
        fragment1 = new Fragment1_Pasture();
        mFragmentCurrent = fragment1;
        fragment2 = new Fragment2_Inventory();
        fragment3 = new Fragment3_Event();

        mFm = getSupportFragmentManager();
        mFt = mFm.beginTransaction();
        mFt.add(R.id.framelayout_main, fragment1);
        mFt.commit();

        ivBack = findViewById(R.id.ivBack);
        ivBack.setVisibility(View.GONE);
        textView_title = (TextView) findViewById(R.id.tvTitle);
        textView_f1 = (TextView) findViewById(R.id.textView_f1);
        textView_f2 = (TextView) findViewById(R.id.textView_f2);
        textView_f3 = (TextView) findViewById(R.id.textView_f3);
        textView_f1.setOnClickListener(this);
        textView_f2.setOnClickListener(this);
        textView_f3.setOnClickListener(this);

        switchContent(fragment1);
        textView_f1.setTextColor(getResources().getColor(R.color.blu));
        textView_title.setText(R.string.pasture);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.textView_f1:
                switchContent(fragment1);
                textView_f1.setTextColor(getResources().getColor(R.color.blu));
                textView_title.setText(R.string.pasture);
                break;
            case R.id.textView_f2:
                switchContent(fragment2);
                textView_f2.setTextColor(getResources().getColor(R.color.blu));
                textView_title.setText(R.string.inventory);
                break;
            case R.id.textView_f3:
                switchContent(fragment3);
                textView_f3.setTextColor(getResources().getColor(R.color.blu));
                textView_title.setText(R.string.mine);
                break;
        }
    }

    public void switchContent(Fragment to) {
//        Log.e("switch",""+to.getId());
        textView_f1.setTextColor(getResources().getColor(R.color.gre));
        textView_f2.setTextColor(getResources().getColor(R.color.gre));
        textView_f3.setTextColor(getResources().getColor(R.color.gre));

        if (mFragmentCurrent != to) {
            mFt = mFm.beginTransaction();
            if (!to.isAdded()) {    //
                mFt.hide(mFragmentCurrent).add(R.id.framelayout_main, to).commit(); //
            } else {
                mFt.hide(mFragmentCurrent).show(to).commit(); //
            }
            mFragmentCurrent = to;
        }
    }

    @Override
    public void onBackPressed() {
        if (exitApp()) {
            exit();
        }
    }

    public void exit() {
        //退出程序，停止所有activity
        //AppManager.getAppManager().appExit(this);
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

    /**
     * [退出程序]
     */
    public boolean exitApp() {
        //判断2次点击事件时间
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            ToastUtils.showShort("再按一次退出程序");
            exitTime = System.currentTimeMillis();
            return false;
        } else {
            return true;
        }
    }
}
